#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/epoll.h>

#define MAXLINE 10
int main(int argc,char* argv[])
{
    int efd,i;
    int pfd[2];
    pid_t pid;
    char buf[MAXLINE],ch = 'a';
    pipe(pfd);
    pid = fork();
    if(pid < 0)
    {
        perror("fork()");
        exit(1);
    }
    else if(pid == 0)
    {
        close(pfd[0]);
        while(1)
        {
            for(i=0;i<MAXLINE/2;i++)
            {
                buf[i] = ch;
            }
            buf[i-1] = '\n';
            ch++;
            for(;i<MAXLINE;++i)
            {
                buf[i] = ch;
            }
            buf[i-1] = ch;
            ch++;
            write(pfd[1],buf,sizeof(buf));
            sleep(5);
        }
        close(pfd[1]);
    }
    else
    {
        close(pfd[1]);
        int res,len;
        struct epoll_event event;
        struct epoll_event resevent[2];
        efd = epoll_create(2);
        if(efd < 0)
        {
            perror("epoll_create()");
            exit(1);
        }
        //event.events = EPOLLIN;             //LT(水平触发,默认)
        event.events = EPOLLIN | EPOLLET;   //ET(边沿触发)
        event.data.fd = pfd[0];
        epoll_ctl(efd,EPOLL_CTL_ADD,pfd[0],&event);
        while(1)
        {
            res = epoll_wait(efd,resevent,2,-1);
            {
                if(resevent[0].data.fd == pfd[0])
                {
                    len = read(pfd[0],buf,MAXLINE/2);
                    write(STDOUT_FILENO,buf,len);
                }
            }
        }
        close(pfd[0]);
        close(efd);
    }
    return 0;
}